<template>
  <div>
    <!-- 导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入要查找的人名"
            v-model="queryInfo.userName"
            @clear="getUserList"
            clearable
          >
            <el-button
              type="primary"
              slot="append"
              icon="el-icon-search"
              @click="findUser"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            @click="addDialogVisible = true"
            v-if="role === 'admin'"
          >添加用户</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table
        :data="userList"
        border
        stripe
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="姓名"
          prop="userName"
        ></el-table-column>
        <el-table-column
          label="角色"
          prop="role"
        ></el-table-column>
        <el-table-column
          label="操作"
          v-if="role === 'admin'"
        >
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              v-if="scope.row.role === 'user'"
              @click="showEditDialog(scope.row)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              v-if="scope.row.role === 'user'"
              @click="removeUserByName(scope.row.userName)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加用户的对话框 -->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item
          label="用户名"
          prop="userName"
        >
          <el-input v-model="addForm.userName"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addUser"
        >确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改用户的对话框 -->
    <el-dialog
      title="修改用户"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item
          label="用户名"
          prop="userName"
        >
          <el-input v-model="editForm.userName"></el-input>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="editUserInfo"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      userList: [],
      role: 'user',
      // 添加用户表单数据
      addForm: {
        userName: '',
      },
      // 添加表单的验证规则对象
      addFormRules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
      },
      // 查询参数
      queryInfo: {
        userName: '',
      },
      // 编辑的用户信息对象
      editForm: {},
      // 修改表单的验证规则对象
      editFormRules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
      },
    };
  },
  mounted() {
    this.getUserList();
  },
  methods: {
    // 获取用户列表
    async getUserList() {
      const { data: res } = await this.$http.get('/api/users', {
        params: {
          userName: window.sessionStorage.getItem('userName'),
        },
      });
      if (res.status === 200) {
        this.userList = res.usersList;
        console.log(this.userList);
        this.role = res.role;
        // this.$message.success(res.msg);
      }
    },

    // 监听添加用户对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },

    // 点击按钮，添加新用户
    addUser() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        this.$set(this.addForm, 'role', 'user');
        let { data: res } = await this.$http.post('/api/addUser', this.addForm);
        if (res.status === 200) {
          this.$message.success(res.msg);
          // 重新获取用户列表数据
          this.getUserList();
          // 隐藏添加用户的对话框
          this.addDialogVisible = false;
        } else {
          this.$message.error(res.msg);
        }
      });
    },

    // 删除用户
    async removeUserByName(userName) {
      let { data: res } = await this.$http.delete('/api/deleteUser', {
        params: {
          userName,
        },
      });
      if (res.status === 200) {
        this.$message.success(res.msg);
        this.getUserList();
      }
    },
    // 展示编辑框
    showEditDialog(value) {
      this.editForm = value;
      this.editDialogVisible = true;
    },
    // 监听修改用户对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },

    // 编辑用户信息
    editUserInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        let { data: res } = await this.$http.put(
          '/api/editUser/',
          this.editForm,
        );
        if (res.status === 200) {
          this.getUserList();
          this.editDialogVisible = false;
          this.$message.success(res.msg);
        }
      });
    },

    // 查找用户
    async findUser() {
      if (this.queryInfo.userName == '') {
        this.$message.error('请输入要查找的姓名');
        this.getUserList();
        return;
      }

      let { data: res } = await this.$http.get('/api/findUser/', {
        params: { userName: this.queryInfo.userName },
      });
      if (res.status === 200) {
        if (res.userList.length > 0) {
          this.userList = res.userList;
          this.$message.success(res.msg);
        } else {
          this.$message.error('没有查找到该用户');
        }
      }
    },
  },
};
</script>
<style lang="less" scoped></style>
